package com.example.nonacademic_education.controller;

import cn.dev33.satoken.annotation.SaCheckRole;
import com.example.nonacademic_education.constant.Constant;
import com.example.nonacademic_education.dao.SqlBackupsDao;
import com.example.nonacademic_education.util.R;
import com.spire.doc.Document;
import com.spire.doc.FileFormat;
import org.springframework.http.MediaType;
import org.springframework.http.MediaTypeFactory;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.net.URLEncoder;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("yw")
public class YWController {
    @Resource
    private SqlBackupsDao sqlBackupsDao;
    @GetMapping("getUsableSpace")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public R getUsableSpace() {
        Map<String, Object> data = new HashMap<>();
        File diskPartition = new File("/home/");
        long usableSpace = diskPartition.getUsableSpace();
        long totalSpace = diskPartition.getTotalSpace();
//        data.put("usableSpace", usableSpace / Constant.B_TO_G);
        data.put("usableSpace", (totalSpace-usableSpace) / Constant.B_TO_G);
        data.put("totalSpace", totalSpace / Constant.B_TO_G);
        return R.ok(data, "");
    }

    @GetMapping("getSqlFileList")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public R getSqlFileList(Integer page, Integer size) {
        if (page < 1) {
            return R.error("page couldn't less than 1");
        } else {
            page = (page - 1) * size;
            Map<String, Object> data = new HashMap<>();
            data.put("list", sqlBackupsDao.getList(page,size));
            data.put("total", sqlBackupsDao.count());
            return R.ok(data, "getSqlFileList");
        }
    }

    @GetMapping("downloadSqlFile")
    //@SaCheckRole(Constant.SUPER_ADMIN)
    public void downloadSqlFile(String path, HttpServletResponse response) throws IOException {
        response.setHeader("Access-Control-Expose-Headers", "Content-Disposition");
        //https://fxljy.cwnu.edu.cn:80/yw/downloadSqlFile?path=backups/backups_2023-10-04.sql
        String p = Constant.proBiologyImagePath + Constant.FILE_PATH;
        File file = new File(p + path);
        long length = file.length();
        response.addHeader("Content-Length", String.valueOf(length));
//        response.addHeader("Content-Disposition",
//                "attachment;filename=" + URLEncoder.encode(path, "utf-8"));
        //表示以二进制流的形式返回
//        response.setContentType("application/octet-stream");
        //设置Content-Disposition为inline
        response.addHeader("Content-Disposition", "attachment;filename="
                + URLEncoder.encode(path.split("/")[1], "utf-8").replaceAll("\\+", "%20"));
        //下面这行代码是解决浏览器预览文件时中文乱码的
        MediaType mediaType = MediaTypeFactory.getMediaType("a.sql").get();
        response.setContentType(mediaType.getType() + ";charset=UTF-8");
        response.addHeader("Content-Length", String.valueOf(length));
        OutputStream outputStream = response.getOutputStream();
        byte[] buff = new byte[1024];
        BufferedInputStream bis = null;
        FileInputStream inputStream = new FileInputStream(file);
        bis = new BufferedInputStream(inputStream);
        int i = bis.read(buff);
        while (i != -1) {
            outputStream.write(buff, 0, buff.length);
            outputStream.flush();
            i = bis.read(buff);
        }
        bis.close();
        outputStream.close();
    }
}
